import { test, expect } from 'bun:test'
/* 自行创建 */
import { scriptToken, webhookURL } from './const-value.ignore'
import { initRemoteCallWpsjsGlobal } from '../index'

test('字段 创建', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL, printLog: true })
  const infoArray = await wpsjsGlobal.Application.Field.CreateFields({
    SheetId: 1,
    Fields: [
      {
        type: 'MultiLineText',
        name: 'MultiLineText',
      },
    ]
  }).execution()
  const info = infoArray[0]
  const keys = ['id', 'name', 'type']
  expect(Object.keys(info)).toEqual(expect.arrayContaining(keys))
})

test('字段 创建 全字段', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL })
  const infoArray = await wpsjsGlobal.Application.Field.CreateFields({
    SheetId: 1,
    Fields: [
      { type: 'MultiLineText', name: 'MultiLineText', },
      { type: 'Date', name: 'Date', numberFormat: 'yyyy"年"M"月"d"日" aaaa hh:mm;@' },
      { type: 'Time', name: 'Time', numberFormat: '[$-409]h:mm AM/PM;@', },
      { type: 'Number', name: 'Number', numberFormat: '#,##0.00_ ' },
      { type: 'Currency', name: 'Currency', numberFormat: '￥#,##0.00;￥-#,##0.00' },
      { type: 'Percentage', name: 'Percentage', numberFormat: '0.0000%' },
      { type: 'ID', name: 'ID' },
      { type: 'Phone', name: 'Phone', },
      { type: 'Email', name: 'Email', },
      { type: 'Url', name: 'Url' },
      { type: 'Checkbox', name: 'Checkbox', },
      { type: 'SingleSelect', name: 'SingleSelect', items: [{ value: 'a' }, { value: 'b' }] },
      { type: 'MultipleSelect', name: 'MultipleSelect', items: [{ value: 'a' }, { value: 'b' }], },
      { type: 'Rating', name: 'Rating', max: 4 },
      { type: 'Complete', name: 'Complete', defaultValue: '0.99', numberFormat: '0%', },
      { type: 'Contact', name: 'Contact', multipleContacts: true, noticeNewContact: false, defaultValueType: 'Normal' },
      { type: 'Attachment', name: 'Attachment', displayStyle: 'List', onlyUploadByCamera: true, },
      { type: 'Link', name: 'Link', linkSheet: 5, multipleLinks: true, },
      { type: 'Note', name: 'Note', },
      { type: 'AutoNumber', name: 'AutoNumber', numberFormat: '00000000' },
      { type: 'CreatedBy', name: 'CreatedBy' },
      { type: 'CreatedTime', name: 'CreatedTime', },
      { type: 'Formula', name: 'Formula' },
      {
        type: 'Lookup',
        name: 'Lookup',
        filter: {
          "groups": [{
            "conds": [
              {
                "condOpType": "Equals",
                "linkSheetFieldId": "K",
                "curSheetCondContents": ["Bt"],
                "curSheetCondType": "Field",
              }
            ]
          }
          ]
        },
        "lookupField": "J",
        "lookupSheetId": 5,
        "lookupFunction": "Origin",
      }
    ]
  }).execution()
  expect(infoArray.length).toBeGreaterThan(0)
})

test('字段 获取', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL, printLog: true })
  const infoArray = await wpsjsGlobal.Application.Field.GetFields({ SheetId: 1 }).execution()
  const Ids = infoArray.map(x => x.id)
  Ids.shift()
  console.log(Ids)/* 可以删除的字段id */
  expect(infoArray.length).toBeGreaterThan(0)
})

test('字段 更新', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL })
  const infoArray = await wpsjsGlobal.Application.Field.UpdateFields({
    SheetId: 1,
    Fields: [
      {
        id: 'Bt',
        name: 'Url',
        type: 'Url',
      },
    ]
  }).execution()
  expect(infoArray.length).toBeGreaterThan(0)
})

test('字段 删除', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL })
  const infoArray = await wpsjsGlobal.Application.Field.DeleteFields({
    SheetId: 1,
    FieldIds: ["D8",]
  }).execution()
  const keys = ['id', 'deleted']
  expect(Object.keys(infoArray[0])).toEqual(expect.arrayContaining(keys))
})
